package com.wswy.wzcx.module.weather

import android.net.Uri
import android.util.Log
import com.facebook.common.util.UriUtil
import com.wswy.wzcx.AppContext
import com.wswy.wzcx.R
import com.wswy.wzcx.model.weather.CarTips
import com.wswy.wzcx.module.CarTipsLoader
import java.util.*

object WeatherUtils {

    const val TYPE_SUN = 1
    const val TYPE_CLOUD = 2
    const val TYPE_RAIN = 3
    const val TYPE_SNOW = 4
    const val TYPE_FOGGY = 5

    private class WeatherConfig(val icon: Int,
                                val fg: Int,
                                val bg: Int = 0,
                                val wType: Int)


    private val calendar = Calendar.getInstance()
    private var todayTips = HashMap<Int, String>()
    private var createTipsTime = arrayOf(0, 0, 0)
    private val carTips = hashMapOf<String, List<CarTips>>()

    init {

        CarTipsLoader(AppContext.getContext()).loader {

            //Log.e("test", "CarTipsLoader --> ${it.orNull()}");

            it.orNull()?.let {
                carTips.clear()
                carTips.putAll(it)

                try {
                    for (i in 1..5) {
                        carTips[i.toString()]?.random()?.let { tips ->
                            todayTips[i] = tips.content
                        }
                    }
                } catch (e: Exception) {

                }

                calendar.timeInMillis = System.currentTimeMillis()
                createTipsTime[0] = calendar.get(Calendar.YEAR)
                createTipsTime[1] = calendar.get(Calendar.MONTH)
                createTipsTime[2] = calendar.get(Calendar.DAY_OF_MONTH)

            }

        }
    }


    private val indexs = hashMapOf<String, WeatherConfig>().apply {

        put("晴", WeatherConfig(0, 0, R.drawable.weather_bg1, TYPE_SUN))
        put("多云", WeatherConfig(1, 1, R.drawable.weather_bg1, TYPE_CLOUD))
        put("阴", WeatherConfig(2, 2, R.drawable.weather_bg2, TYPE_CLOUD))
        put("阵雨", WeatherConfig(3, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("雷阵雨", WeatherConfig(4, 4, R.drawable.weather_bg3, TYPE_RAIN))
        put("雷阵雨并伴有冰雹", WeatherConfig(5, 4, R.drawable.weather_bg3, TYPE_RAIN))
        put("雨夹雪", WeatherConfig(6, 3, R.drawable.weather_bg2, TYPE_SNOW))
        put("小雨", WeatherConfig(7, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("中雨", WeatherConfig(8, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("大雨", WeatherConfig(9, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("暴雨", WeatherConfig(10, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("大暴雨", WeatherConfig(11, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("特大暴雨", WeatherConfig(12, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("阵雪", WeatherConfig(13, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("小雪", WeatherConfig(14, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("中雪", WeatherConfig(15, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("大雪", WeatherConfig(16, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("暴雪", WeatherConfig(17, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("雾", WeatherConfig(18, 2, R.drawable.weather_bg6, TYPE_FOGGY))
        put("冻雨", WeatherConfig(19, 3, R.drawable.weather_bg2, TYPE_FOGGY))
        put("沙尘暴", WeatherConfig(20, 20, R.drawable.weather_bg5, TYPE_FOGGY))
        put("小雨-中雨", WeatherConfig(8, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("中雨-大雨", WeatherConfig(9, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("大雨-暴雨", WeatherConfig(10, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("暴雨-大暴雨", WeatherConfig(11, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("大暴雨-特大暴雨", WeatherConfig(12, 3, R.drawable.weather_bg2, TYPE_RAIN))
        put("小雪-中雪", WeatherConfig(15, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("中雪-大雪", WeatherConfig(16, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("大雪-暴雪", WeatherConfig(17, 13, R.drawable.weather_bg4, TYPE_SNOW))
        put("浮尘", WeatherConfig(29, 20, R.drawable.weather_bg5, TYPE_FOGGY))
        put("扬沙", WeatherConfig(30, 20, R.drawable.weather_bg5, TYPE_FOGGY))
        put("强沙尘暴", WeatherConfig(31, 20, R.drawable.weather_bg5, TYPE_FOGGY))
        put("飑", WeatherConfig(0, 0, 0, TYPE_FOGGY))
        put("龙卷风", WeatherConfig(0, 0, 0, TYPE_FOGGY))
        put("弱高吹雪", WeatherConfig(0, 0, 0, TYPE_FOGGY))
        put("轻霾", WeatherConfig(53, 2, R.drawable.weather_bg6, TYPE_FOGGY))
        put("霾", WeatherConfig(53, 2, R.drawable.weather_bg6, TYPE_FOGGY))

    }

    private fun getWeatherIcon(weather: String): String {

        return indexs[weather]?.icon?.let {
            toFs(it)
        } ?: ""
    }


    fun getWeatherIconUri(weather: String?, bgWhite: Boolean): Uri? {

        weather ?: return null

        val f = if (bgWhite) "white" else "color"
        return UriUtil.parseUriOrNull("asset:///image/weather/$f/ic_weather_${getWeatherIcon(weather)}.png")
    }


    fun getWeatherBg(weather: String?): Int {
        weather ?: return 0

        return indexs[weather]?.bg ?: 0
    }

    fun getWeatherFg(weather: String?): Uri? {
        weather ?: return null

        val id = indexs[weather]?.fg?.let {
            toFs(it)
        } ?: ""

        return UriUtil.parseUriOrNull("asset:///image/weather/bg/weather_bg_$id.png")
    }

    private fun getWeatherTipsType(weather: String?): Int {
        weather ?: return TYPE_SUN

        return indexs[weather]?.wType ?: TYPE_SUN
    }


    fun getWeatherTips(weather: String?): String {
        val type = getWeatherTipsType(weather)


        calendar.timeInMillis = System.currentTimeMillis()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        if (year == createTipsTime[0] && month == createTipsTime[1] && day == createTipsTime[2]) {
            return todayTips[type] ?: ""
        } else {

            for (i in 1..5) {
                carTips[i.toString()]?.random()?.let { tips ->
                    todayTips[i] = tips.content
                }
            }
            createTipsTime[0] = year
            createTipsTime[1] = month
            createTipsTime[2] = day

            return todayTips[type] ?: ""
        }


//        if (createTipsTime == 0L){
//            createTipsTime = System.currentTimeMillis()
//        }

        //return todayTips[type]?:""

    }

    private fun toFs(id: Int) = if (id < 10) "0$id" else id.toString()


}